/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-04-20
 * Time: 14:49
 */

// 线程安全问题

public class Counter {
    private int count = 0;

//   public void add() {
//       synchronized (this) {
//            count++;
//        }
//    }
//    synchronized public void add() {
//        count++;
//    }

//    synchronized public static void test1() {
//
//    }

    private Object locker = new Object();

    public void add() {
        synchronized (locker) {
            count++;
        }
    }

   //直接给方法使用synchronized修饰此时相当于以this为锁对象
     //synchronized public void add() {
            //count++;
    //}

    public int getCount() {
        return count;
    }


}

